package com.dh.calendar;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarDemo01 {
    public static void main(String[] args) throws ParseException {

        // 通过getInstance 获取日历对象
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH) + 1);
        System.out.println(calendar.get(Calendar.DATE));
        System.out.println(calendar.get(Calendar.HOUR));
        System.out.println(calendar.get(Calendar.MINUTE));
        System.out.println(calendar.get(Calendar.SECOND));
//
//        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));// 今年的第几天
//        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));// 这个月的第几天
//        System.out.println(calendar.get(Calendar.DAY_OF_WEEK) - 1);// 这周的几天


        // 格式化对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 创建一个日历对象
        Calendar calendar1 = Calendar.getInstance();
        // 把日历对象设置一个时间
        calendar1.setTime(sdf.parse("2021-01-29 14:45:00"));


        // 获取当前的一个时间
        Calendar now = Calendar.getInstance();

        System.out.println(sdf.format(now.getTime()));//2023-03-06 10:18:14
        System.out.println(sdf.format(calendar1.getTimeInMillis()));//2021-01-29 14:45:00
        System.out.println(calendar1.before(now));//用于检查此日历时间是否早于给定对象的时间表示的时间。

    }
}
